<?php namespace Cms\Api;

use Hdphp\Api\Api;

use Cms\Model\Menu as MenuModel;

class Menu extends Api
{
	protected $db;

	public function __construct()
	{
		$this->db = new MenuModel;
	}

	/**
	 * 获得顶级菜单
	 * @return [type] [description]
	 */
	public function getTopMenu()
	{
		$data = Db::table( 'menu' )->where( 'pid', 0 )->orderBy( 'sort', 'ASC' )->get();

		foreach($data as $k=>$d)
		{
			$data[$k]['url']=__WEB__.'/cms/index?id='.$d['id'];
		}
		return $data;
	}
	/**
	 * 获取指定pid的左侧菜单
	 *
	 * @param  [type] $id [description]
	 *
	 * @return [type]     [description]
	 */
	public function getLeftMenu()
	{
		//顶级菜单ID
		if(!$id = Q('get.id'))
		{
			$id = Db::table('menu')->orderBy('sort','ASC')->where('pid',0)->pluck('id');
		}

		$menu = Db::table('menu')->orderBy('sort','ASC')->get();

		$list = array();
		foreach((array) $menu as $k=>$m)
		{
			if ($m['pid']==$id)
			{
				$list[$k]=$m;
				foreach((array) $menu as $n)
				{
					//三级菜单
					if($n['pid']==$m['id'])
					{
						if(!empty($n['url']))
						{
							$n['url']='/'.$n['url'];
							
							//参数
							if(!empty($n['param']))
							{
								$n['url']="?".$n['params'];
							}
						}
						else
						{
							$n['url']=__WEB__."?m={$n['module']}&c={$n['controller']}&a={$n['action']}";
							//参数
							if(!empty($n['param']))
							{
								$n['url'].="&".$n['params'];
							}
						}
						$list[$k]['_SUB_'][]=$n;

					}
				}
			}
		}

		return $list;
	}

	/**
	 * 获取指定ID的菜单
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function get($id)
	{
		return Db::table( 'menu' )->where( 'id', $id )->first();
	}
	/**
	 * 获取多级菜单
	 * @param string $id
	 *
	 * @return mixed
	 */
	public function getLevelMenu( $id = '' )
	{
		$menu = Db::table( 'menu' )->orderBy( 'sort', 'ASC' )->get();
		$menu = Data::tree( $menu, 'title', 'id', 'pid' );

		return $menu;
	}

	/**
	 * 判断 $id 是 $pid 的子栏目
	 *
	 * @param  [type]  $id
	 * @param  [type]  $pid
	 *
	 * @return boolean
	 */
	public function isChildMenu( $id, $pid )
	{
		$menu = Db::table( 'menu' )->orderBy( 'sort', 'ASC' )->get();
		if ( Data::isChild( $menu, $id, $pid, $fieldPri = 'id', $fieldPid = 'pid' ) )
		{
			return true;
		}
	}

	/**
	 * 删除菜单
	 *
	 * @param  [type] $id [description]
	 *
	 * @return [type]     [description]
	 */
	public function destroy( $id )
	{
		//是否有子菜单
		if ( Db::table( 'menu' )->where( 'pid', $id )->first() )
		{
			$this->error = '请先删除子菜单';

			return false;
		}

		return $this->db->delete($id);
	}

	//更新菜单
	public function update($id)
	{
		if ($this->db->create())
		{
			if ($this->db->save() === true )
			{
				return true;
			}
			else
			{
				p($this->db->getQueryLog());
				$this->error = $this->db->getError();
			}
		}
		else
		{
			$this->error = $this->db->getError();

			return false;
		}
	}

	//新增菜单
	public function store()
	{
		if ( $this->db->create() )
		{
			if ( $this->db->add() === true )
			{
				return true;
			}
		}
		else
		{
			$this->error = $this->db->getError();

			return false;
		}
	}
}